/**
 * author : weiki
 * date : 2017/0928
 * 查看拼团订单页
 * 
 */

var app = getApp();

Page({

	data: {
		order: {},
		imgUrl: app.urlimg, //图片文件url前缀
		title: {}, //头部红色区域显示的说明性文字
		succeed_num: '', //组团成功后的类别判断
		refunds: false,  // 是否显示 申请退款 按钮
		goodsInfo: {},  //页面中间部分商品信息
		readFinish: false, //判断页面是否获取数据完成
	},

	/**
	 * 获取订单详细信息
	 */
	getOrderInfo: function (orderId) {
		var that = this;
		var refunds = false; //对应 this.data.refunds
		app.getUserInfo(function (token_key) {
			wx.request({
				url: app.url + "api_team/getTeamOrderById",
				data: {
					orderId: orderId
				},
				header: {
					"content-type": "application/json",
					'token': token_key
				},
				success: function (res) {
					console.log(res.data);
					var status = res.data.result.status; //状态说明:1为团人数不足 2为成团待发货  3为成团已发货 4为拼团失败退款中  5为拼团失败退款完成
					var title = {};   //对应 data.title字段
					var group_type = res.data.result.regiment_info.group_type;
					var is_draw = (group_type == 2 || group_type == 3) ? true : false;
					switch (status.status) {
						case 1:
							title = {
								max_title: '拼团还未成功',
								min_title: '让小伙伴们都来拼团吧~',
								img: '/pages/icon/pingtuan/04.png',
								img_style: 'width:70px;height: 70px;top:-4px;',
								state: '拼团中...',
							};
							refunds = false;
							break;
						case 2:
							if (is_draw) {
								if (res.data.result.regiment_info.isPrize == '1'){
									var stateText = "已成团，已开奖", img = '/pages/icon/pingtuan/02.png', img_style = 'width:70px;height: 70px;top:-4px;';
									var prize = res.data.result.prize;
									var min_title = '';
									if (prize == 1) {
										min_title = '恭喜，已中一等奖，大奖马上到！';
									} else if (prize == 2) {
										min_title = '抱歉，没中奖哦。即将安排退款再送你专属劵';
									} else if (prize == 3) {
										min_title = '恭喜，已中二等奖，即将安排退款并送礼品！';
									}
								}else{
									var stateText = '已成团，待开奖', min_title = '拼团成功，请耐心等待开奖~', img = '/pages/icon/pingtuan/04.png',img_style='width:70px;height: 70px;top:-4px;';
								}
							} else {
								var stateText = "已成团，待发货", min_title ='仓库正为您的商品打包发货，请耐心等待', img = '/pages/icon/pingtuan/04.png',img_style='width:70px;height: 70px;top:-4px;';
							}
							title = {
								max_title: stateText,
								min_title: min_title,
								img: img,
								img_style: img_style,
								state: stateText,
							};
							refunds = true;
							break;
						case 3:
							var min_title = '距离自动确认收货还剩' + time_computations(res.data.result.distance_accept_time, that);
							title = {
								max_title: '卖家已发货',
								min_title: min_title,
								img: '/pages/icon/pingtuan/01.png',
								img_style: 'width:70px;height: 44px;top:10px;',
								state: '待收货',
							};
							refunds = true;
							break;
						case 4:
							title = {
								max_title: '拼团失败，退款中',
								min_title: '预计1-7个工作日内处理退款',
								img: '/pages/icon/pingtuan/03.png',
								img_style: 'width:70px;height: 70px;top:-4px;',
								state: '拼团失败，退款中',
							};
							break;
						case 5:
							title = {
								max_title: '拼团失败，退款完成',
								min_title: '',
								img: '/pages/icon/pingtuan/03.png',
								img_style: 'width:70px;height: 70px;top:-4px;',
								state: '拼团失败，退款完成',
							};
							break;
                        case 7:
                            title = {
                                max_title: '申请退款',
                                min_title: '预计1-7个工作日内处理退款',
                                img: '/pages/icon/pingtuan/03.png',
                                img_style: 'width:70px;height: 70px;top:-4px;',
                                state: '申请退款'
                            };
                            break;
                        case 8:
                            title = {
                                max_title: '退款失败',
                                min_title: '请联系骆驼官方客服',
                                img: '/pages/icon/pingtuan/03.png',
                                img_style: 'width:70px;height: 70px;top:-4px;',
                                state: '请联系骆驼官方客服'
                            };
                            break;
                        case 9:
                            title = {
                                max_title: '退款成功',
                                min_title: '款项已原路返回了哦，请查收',
                                img: '/pages/icon/pingtuan/03.png',
                                img_style: 'width:70px;height: 70px;top:-4px;',
                                state: '退款成功'
                            };
                            break;
						default:
							break;
					}

					if (res.data.result.team_first == 1 && (res.data.result.team_sign.indexOf("mdt") > -1)) {
						refunds = false;
					}

					if (res.data.result.status.prize > 0) {
						refunds = true;
					}

					console.log(title);
					var team_type_text = '';
					if (res.data.result.team_sign.indexOf("pt-") > -1) {
						team_type_text = '拼团';
					}
					if (res.data.result.team_sign.indexOf("cjtm") > -1) {
						team_type_text = '会员抽奖团';
					}
					if (res.data.result.team_sign.indexOf("cjt") > -1) {
						team_type_text = '抽奖团';
					}
					if (res.data.result.team_sign.indexOf("mdt") > -1) {
						team_type_text = '免单团';
					}
					that.setData({
						is_draw: is_draw,
						readFinish: true,
						team_type_text: team_type_text,
						title: title,
						succeed_num: status.status,
						order: res.data.result,
						goodsInfo: res.data.result.goods,
						refunds: refunds
          })


					//拼团推荐
					wx.request({
						url: app.url + 'api_team/recommend',
						data: {
							goods_id: res.data.result.goods[0].goods_id,
							// goods_id: 2829,
						},
						header: {
							'Content-Type': 'application/json',
							'token': wx.getStorageSync('token_key')
						},
						success: function (res) {
							console.log(res.data.result);
							that.setData({
								goods_lists: res.data.result,
							})

						},
						// 接口调用失败返回的函数
						fail: function (err) {
							console.log(err)
						}
					})
				}
			})
		});
	},
	//查看物流
	logistics: function (e) {
		wx.navigateTo({
			url: '/pages/account/logistics/index?id=' + e.currentTarget.dataset.order_id,
		})
	},
	/**
 * 查看中奖详情
 */
	prizeTap: function (e) {
		var order_id = e.currentTarget.dataset.order_id
		if (order_id > 0) {
			wx.navigateTo({
				url: '/pages/mygroup/winning/winning?orderId=' + order_id,
			})
		}
	},
	// 查看商品详情
	purchase: function (e) {
		var that=this;
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + e.currentTarget.dataset.goods_id + '&regiment_id=' + that.data.order.regiment_id + "&group_type=" + that.data.order.regiment_info.group_type,
		})
	},
	//退款申请
	refunds: function (e) {

        if (this.data.order.str_completion_time) {
            var now = Date.parse(new Date()) / 1000;
            var temp = this.data.order.str_completion_time + 86400 * 30;
            if (now > temp) {
                wx.showModal({
                    title: '提示',
                    content: '因收货时间较长，不支持申请售后，有问题请联系在线客服',
                    success: function (res) {
                        wx.redirectTo({
                            url: '/pages/index/index'
                        })
                    }
                })
            } else {
                wx.redirectTo({
                    url: '/pages/account/refund/index?order_goods_id=' + e.currentTarget.dataset.id + '&order_id=' + e.currentTarget.dataset.order_id,
                })
            }
        } else {
            wx.redirectTo({
                url: '/pages/account/refund/index?order_goods_id=' + e.currentTarget.dataset.id + '&order_id=' + e.currentTarget.dataset.order_id,
            })
        }


	},
	/**
	 * 点击复制订单编号
	 */
	cpOrderNo: function () {
		var that = this;
		var orderNo = that.data.order.order_no;
		wx.setClipboardData({
			data: orderNo,
			success: function (res) {
				wx.showToast({
					title: '复制成功',
				});
			}
		})
	},

	/**
	 * 查看团详情
	 */
	inviteClick: function () {
		console.log(this.data.order.id);
		wx.navigateTo({
			url: '../invite/invite?orderId=' + this.data.order.id,
		});
	},
	//返回首页
	homeTAP: function () {
		wx.redirectTo(
			{
				url: '/pages/index/index',
				success: function (res) {
					clearInterval(timer);
				}
			}
		)
	},
	// 进入商品详情页面(去开团)
	click: function (e) {
		console.log(e.currentTarget);
		wx.redirectTo({
			url: '/pages/group/groupdetail/index?id=' + e.currentTarget.dataset.index + '& regiment_id=' + e.currentTarget.dataset.regiment_id + '& group_type=' + e.currentTarget.dataset.group,
		})
	},
	//确认收货
	confirm: function (e) {
		var that = this;
		wx.showModal({
			title: '提示',
			content: '请收到货后，再确认收货！',
			success: function (res) {
				if (res.confirm) {
					wx.request({
						url: app.url + 'api_user/order_status',
						data: {
							order_id: e.currentTarget.dataset.id,
							op: 'confirm'
						},
						header: {
							'content-type': 'application/json',
							'token': wx.getStorageSync('token_key')
						}, success: function (json) {
							if (json.data.done == true) {
								that.getData();
							} else {
								wx.showModal({
									title: '提示',
									content: json.data.message,
								})
							}
						}
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 * options.orderId : 订单ID
	 */
	onLoad: function (options) {

		var title = '';
		var refunds = false;//退款按钮的隐藏与显示
		this.setData({
			refunds: refunds
		});

		//获取订单信息
		this.getOrderInfo(options.orderId);
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		return {
			title: "我在骆驼商城玩拼图，你也来参加！！！",
			path: "",
			success: function (res) {

			},
			fail: function (res) {

			}
		};
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () { },

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () { },

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () { },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () { },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () { },

	submitAct: function (e) {
		wx.request({
			url: app.url + "api_team/collectFormId",
			data: {
				formId: e.detail.formId
			},
			header: {
				'content-type': 'application/json',
				'token': wx.getStorageSync('token_key')
			},
			success: function (result) { }
		})
	}

})
//倒计时计算
function time_computations(Time, that) {
	var current_time = Date.parse(new Date()) / 1000;
	// console.log("获取当前时间" + current_time);
	var time_text = ''; 1512032915
	if (current_time < Time) {
		var Times = Time - current_time;
		--Times;

		var day = Math.floor(Times / (60 * 60 * 24));//计算剩余的天数
		if (('' + day).length == 1) {
			day = '0' + day;
		}
		var hours = parseInt(Times / 60 / 60 % 24, 10); //计算剩余的小时
		if (('' + hours).length == 1) {
			hours = '0' + hours;
		}
		var minutes = parseInt(Times / 60 % 60, 10);//计算剩余的分钟
		if (('' + minutes).length == 1) {
			minutes = '0' + minutes;
		}
		var seconds = parseInt(Times % 60, 10);//计算剩余的秒数
		if (('' + seconds).length == 1) {
			seconds = '0' + seconds;
		}
		//倒计时判断
			return time_text = day + "天" + hours + "小时";
	} else {
		return false;
	}
}